|
Download
FAQ History |
|
API
Search Feedback |
A J2EE Application that Uses the JMS API with a Session Bean
This section explains how to write, compile, package, deploy, and run a J2EE application that uses the JMS API in conjunction with a session bean. The application contains the following components:
The section covers the following topics:
You will find the source files for this section in the directory
<INSTALL>/j2eetutorial14/examples/jms/clientsessionmdb/. Path names in this section are relative to this directory.Writing the Application Components
This application demonstrates how to send messages from an enterprise bean--in this case, a session bean--rather than from an application client, as in the example in A Message-Driven Bean Example. Figure 21-2 illustrates the structure of this application.
![]()
Figure 21-2 A J2EE Application: Client to Session Bean to Message-Driven Bean
The Publisher enterprise bean in this example is the enterprise-application equivalent of a wire-service news feed that categorizes news events into six news categories. The message-driven bean could represent a newsroom, where the Sports desk, for example, would set up a subscription for all news events pertaining to sports.
The application client in the example obtains a handle to the Publisher enterprise bean's remote home interface and calls the enterprise bean's business method. The enterprise bean creates 18 text messages. For each message, it sets a
Stringproperty randomly to one of six values representing the news categories and then publishes the message to a topic. The message-driven bean uses a message selector for the property to limit which of the published messages it receives.Writing the components of the application involves
Coding the Application Client: MyAppClient.java
The application client program,
src/MyAppClient.java,performs no JMS API operations and so is simpler than the client program in A Simple J2EE Application that Uses the JMS API. The program obtains a handle to the Publisher enterprise bean's remote home interface, using the JNDI naming contextjava:comp/env. The program then creates an instance of the bean and calls the bean's business method twice.Coding the Publisher Session Bean
The Publisher bean is a stateless session bean with one
createmethod and one business method. The Publisher bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.The remote home interface source file is
src/PublisherHome.java.The remote interface,
src/PublisherRemote.java, declares a single business method,publishNews.The bean class,
src/PublisherBean.java,implements thepublishNewsmethod and its helper methodchooseType. The bean class also implements the required methodsejbCreate,setSessionContext,ejbRemove,ejbActivate, andejbPassivate.The
ejbCreatemethod of the bean class allocates resources--in this case, by looking up theConnectionFactoryand the topic and creating theConnection. The business methodpublishNewscreates aSessionand aMessageProducerand publishes the messages.The
ejbRemovemethod must deallocate the resources that were allocated by theejbCreatemethod. In this case, theejbRemovemethod closes theConnection.Coding the Message-Driven Bean: MessageBean.java
The message-driven bean class,
src/MessageBean.java, is identical to the one in A Simple J2EE Application that Uses the JMS API. However, the deployment descriptor will be different, because the bean is using a topic with a durable subscription, instead of a queue.Creating and Packaging the Application
This example uses the topic named
jms/Topicand the connection factoryjms/TopicConnectionFactory,which you created in Creating the JMS Administered Objects for the Simple Examples.Creating and packaging this application involve several steps:
Compiling the Source Files and Starting the J2EE Application Server
Starting the J2EE Deploytool and Creating the Application
Packaging the Application Client
To package the application client, perform the following steps:
- Choose File
New
Application Client JAR to start the Application Client Wizard.
- In the JAR File Contents screen:
- Verify that Create New AppClient Module in Application is selected and that the application is
ClientSessionMDBApp.- In the AppClient Display Name field, type
MyAppClient.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
build/client/directory. SelectMyAppClient.classfrom the Available Files tree area and click Add, then OK.- In the General screen, choose
client.MyAppClientin the Main Class combo box.- In the Enterprise Bean References screen, click Add. In the dialog box:
- Type
ejb/remote/Publisherin the Coded Name field.- Choose Session from the EJB Type menu.
- Choose Remote from the Interfaces menu.
- Type
sb.PublisherHomein the Home Interface field.- Type
sb.PublisherRemotein the Local/Remote Interface field.- Leave the Enterprise Bean Name field blank.
The client accesses the bean from outside of the EJB container, so the session bean uses remote interfaces that are accessed through the JNDI name.
- When you return to the Enterprise Bean References screen, select the bean. In the Deployment Settings area, select JNDI Name and type
Publisherin the field.- Click Finish.
Packaging the Session Bean
To package the session bean, perform the following steps:
- Choose File
New
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- Select Create New JAR Module in Application and verify that the application is
ClientSessionMDBApp. In the JAR Display Name field, typeEBJAR.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
build/sb/directory. SelectPublisherBean.class,PublisherHome.class, andPublisherRemote.classfrom the Available Files tree area and click Add, then OK.- In the General screen:
- Choose the Session radio button.
- Choose the Stateless radio button.
- From the Enterprise Bean Class menu, choose
sb.PublisherBean.- In the Enterprise Bean Display Name field, type
Publisher.- In the Remote Interfaces area, choose
sb.PublisherHomefrom the Remote Home Interface menu andsb.PublisherRemotefrom the Remote Interface menu.- In the Configuration Options screen, check the Message Destination References, Resource References, and Transaction Management boxes.
- In the Transaction Management screen, choose Container-Managed.
- In the Message Destination References screen, click Add. In the dialog box:
- In the Resource References screen:
- Click Finish.
- Click the
EBJARnode, then click the Message Destinations tab. In the inspector pane:Packaging the Message-Driven Bean
You will package the message-driven bean in the same JAR file as the session bean, for greater efficiency.
To package the message-driven bean, perform the following steps:
- Choose File
New
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- In the General screen:
- In the Configuration Options screen, check the Transaction Management box.
- In the Message-Driven Bean Settings screen:
- Click Add in the Activation Configuration Properties area six times to type the property names and values shown in Table 21-3.
- Choose
javax.jms.Topicfrom the Destination Type menu.- Choose
PhysicalTopicfrom the Destination Name menu.- Click Deployment Settings. In the dialog box, type
jmsrain the Resource Adapter field and click OK.- In the Transaction Management screen, choose Container-Managed.
- Click Finish.
- Click the Message Destinations tab. You will see that
MessageBeannow appears in the Consumers area.The activation configuration property values for the bean are shown in Table 21-3.
Updating the JNDI Names
You need to update the JNDI name for the message-driven bean so that it specifies the destination it receives messages from, not the bean name.
Verify that the JNDI names for the application components are correct. They should appear as shown in Table 21-4 and Table 21-5.
Table 21-4 Application Pane for ClientSessionMDBApp Component Type Component JNDI Name EJB MessageBean jms/Topic EJB Publisher Publisher
Deploying the Application
Saving the Client JAR and Running the Application
- In the deploytool, select the server node.
- Select
ClientSessionMDBAppfrom the list of deployed objects, then click Client Jar.- Choose Browse to navigate to the directory from which you will run the client. Specify the
clientsessionmdbdirectory, click Select, then click OK. Click OK in the information dialog. You will find a file namedClientSessionMDBAppClient.jarin the specified directory.- To run the client, use the following command:
appclient -client ClientSessionMDBAppClient.jarThe program output in the terminal window looks something like this:
The output from the enterprise beans appears in the server log. The Publisher session bean sends two sets of 18 messages numbered 0 through 17. Because of the message selector, the message-driven bean receives only the messages whose NewsType property is Sports or Opinion.
Suppose that the last few messages from the Publisher session bean look like this:
PUBLISHER: Setting message text to: Item 12: Business PUBLISHER: Setting message text to: Item 13: Opinion PUBLISHER: Setting message text to: Item 14: Living/Arts PUBLISHER: Setting message text to: Item 15: Sports PUBLISHER: Setting message text to: Item 16: Living/Arts PUBLISHER: Setting message text to: Item 17: Living/ArtsBecause of the message selector, the last messages received by the message-driven bean will be the following:
If you like, you can rewrite the message selector to receive different messages.
Undeploying and Removing the Application
To undeploy and remove the application, perform the following steps:
If you wish, you can manually delete the EAR and client JAR files.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.